#include "StdAfx.h"
#include "mrc.h"

mrc::mrc(void)
{
}

mrc::~mrc(void)
{
}

int mrc::run(char *ip)
{
	socket server = socket(AF_INET, SOCK_STREAM, 0);
	if (server < 1)
	{
		perror("socket()");
		return -1;
	}

	sockaddr_in sin = {0};
	sin.sin_family = AF_INET;
	sin.sin_port = htons(18888);
	sin.sin_addr.s_addr = htonl(INADDR_ANY);

	if (bind(server, (sockaddr*)&sin, sizeof(sin)) != 0)
	{
		perror("bind()");
		return -1;
	}

	listen(server, 3);

	char msg[] = "hello, i'm server";
	char buf[1024] = {0};
	int buflen = 0;
	while (1)
	{
		sockaddr_in sinc = {0};
		int len = 0;
		socket sc = accept(server, (sockaddr*)sinc, &len);
		send(sc, msg, sizeof(msg), 0);
		recv(sc, &buf, &buflen);
		if (buflen>0&&strcmp(buf, "88") == 0)
		{
			close(sc)
			return 1;
		}
	}


	return 0;
}